package cn.jetpiece.cloud.joss.app.core.utils;

import cn.jetpiece.cloud.core.util.HttpContext;
import cn.jetpiece.cloud.joss.app.core.constant.OssConstants;
import cn.jetpiece.cloud.model.contants.CommonConstant;

import javax.servlet.http.HttpServletRequest;

/**
 * @author XuJZ
 * @since 2020/11/16
 */
public class AuthUtil {

    /**
     * 检验当时是否鉴权
     *
     * @return 鉴权
     */
    public static boolean isNotAuth() {
        return !isJossAuth() && !isFeignAuth();
    }

    private static boolean isJossAuth() {
        HttpServletRequest request = HttpContext.getRequest();
        String isAuth = request.getHeader(OssConstants.JOSS_AUTH_HEADER);
        if (isAuth == null || isAuth.trim().length() < 1) {
            return false;
        }
        return Integer.parseInt(isAuth) == 1;
    }

    private static boolean isFeignAuth() {
        HttpServletRequest request = HttpContext.getRequest();
        String isAuth = request.getHeader(CommonConstant.INTERIOR_SERVICE_INVOCATION);
        if (isAuth == null || isAuth.trim().length() < 1) {
            return false;
        }
        return Integer.parseInt(isAuth) == 1;
    }
}
